﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.ComponentModel;
using AForge.Imaging.Filters;
using BrainTechLLC;
using BrainTechLLC.ImageOperators;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Drawing;
using AForge.Imaging;
using System.Diagnostics;
using BrainTechLLC.ImageOperatorsNonAForgeDependent;

namespace BrainTechLLC.FilterExtensions
{
    [Serializable]
    public class SingleFilter : SingleFilterBase, INotifyPropertyChanged, IObjectLock, IPostProcessor, ISingleFilter
    {
        public SingleFilter() { }

        public SingleFilter(IFilter filter)
        {
            Filter = filter;
        }

        public override string Name
        {
            get { return FilterTypeName; }
        }

        public override UnmanagedImage Apply(string fileName, UnmanagedImage image, bool wantPostProcessInfo, ref List<RGBAndCoordinate> postProcessInfo, ref int width, ref int height)
        {
            postProcessInfo = null;
            width = 0;
            height = 0;

            if (image == null)
                return null;

            if (Disabled)
            {
                return image;
            }

            UnmanagedImage dstImg = image;
            UnmanagedImage tmpImg = null;

            IFilter filterToApply = Filter.MakeCopy(2, 0) as IFilter;

            int tries = 0;

        TryAgain:
            try
            {
                if (!filterToApply.IsValidPixelFormat(dstImg.PixelFormat))
                {
                    List<PixelFormat> formats = filterToApply.GetValidPixelFormats();                    
                    formats[0].TryConvertToUsableFormat(ref dstImg, ref tmpImg);
                }

                filterToApply.HandleGatherPixellateInfoPre(wantPostProcessInfo);

                tmpImg = dstImg;
                try
                {
                    dstImg = filterToApply.Apply(tmpImg);
                    tmpImg.Dispose();
                    tmpImg = null;
                }
                catch (Exception exInner)
                {
                    Debug.WriteLine(exInner.ToString());
                    throw;
                }

                filterToApply.HandleGatherPixellateInfoPost(wantPostProcessInfo, ref postProcessInfo, ref width, ref height, dstImg);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());

                if (tries == 0)
                {
                    List<PixelFormat> formats = Filter.GetValidPixelFormats();
                    
                    if (formats[0].TryConvertToUsableFormat(ref dstImg, ref tmpImg))
                    {
                        tries++;
                        goto TryAgain;
                    }
                }
            }

            return dstImg;
        }
    }
}

